import 'package:flutter/foundation.dart';
import 'package:love_newf/app/com/util/permission_utils.dart';
import 'package:love_newf/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:image_picker/image_picker.dart';

import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'package:love_newf/value/app_colors.dart';
import 'package:love_newf/value/app_lstyle.dart';

bool _debug = kDebugMode;
var itemSy = LStyle(
    height: 64.w,
    alignment: Alignment.center,
    fontSize: 16.sp,
    fontWeight: FontWeight.w500,
    color: AppColors.textColor);

var itemCancelSy = LStyle(
    height: 64.w,
    alignment: Alignment.center,
    fontSize: 16.sp,
    fontWeight: FontWeight.w500,
    color: AppColors.textSecondColor);

class ImageSelectUtils {
  ///返回图片路径,index -1 会提示用户选择，0 表示默认相机，1 表示默认 相册
  static Future<Map?> getFileImagePath({index1 = 0,preferredCameraDevice}) async {
    int index = index1;
    if (_debug) index = -1;
    if (index == -1) {
      index = -2;
      await Get.bottomSheet(
          LColumn(lStyle: AppLStyle.bottomDialogStyle, children: [
        LSizeBoxHeight(10.w),
        InkWell(
            child: LText(S.current.camera, lStyle: itemSy),
            onTap: () {
              index = 0;
              Get.back();
            }),
        InkWell(
            child: LText(S.current.gallery, lStyle: itemSy),
            onTap: () {
              index = 1;
              Get.back();
            }),
        LText(S.current.cancel, lStyle: itemCancelSy, onTap: () {
          index = -2;
          Get.back();
        }),
      ]));
    }

    if (index == -2) return null;
    XFile? image;

    if (index == 0) {
      try {
        image = await ImagePicker().pickImage(
            source: ImageSource.camera,
            maxWidth: 1080 / 2,
            maxHeight: 2040 / 2,preferredCameraDevice: preferredCameraDevice??CameraDevice.rear);
      } catch (e) {
        await PermissionUtils.checkCameraPermission();
        return null;
      }
    } else {
      image = await ImagePicker().pickImage(
          source: ImageSource.gallery, maxWidth: 1080 / 2, maxHeight: 2040 / 2);
    }
    if (image == null) return null;
    // PrintUtils.print1("image length: ",( await image.readAsBytes()).lengthInBytes.toString());
    // return null;
    return {"path": image.path, "mode": index};
  }
}
